package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.MarketingSettingService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.marketing.pojo.dto.MarketingSettingDetailDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingSettingListDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingSettingSaveDTO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingSettingDetailVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingSettingListVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingSettingSaveVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author jyh
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/marketingSettings")
public class MarketingSettingController {


    @Resource
    private MarketingSettingService marketingSettingService;


    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid MarketingSettingListDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingSettingListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingSettingListVO marketingManagementVO = marketingSettingService.getList(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDetail(@RequestBody @Valid MarketingSettingDetailDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingSettingDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingSettingDetailVO marketingManagementVO = marketingSettingService.getDetail(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid MarketingSettingSaveDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingSettingSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingSettingSaveVO marketingManagementVO = marketingSettingService.save(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
